-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.py
More file actions
34 lines (29 loc) · 1.14 KB
/
Solution.py
File metadata and controls
34 lines (29 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def dfs(v, graph, visited, stack):
"""Recursive DFS that marks visited nodes and appends vertices to stack."""
visited[v] = True
for neighbor in graph[v]:
if not visited[neighbor]:
dfs(neighbor, graph, visited, stack)
stack.append(v)
def topological_sort(graph, vertices):
"""Returns a list of vertices in topologically sorted order."""
visited = [False] * vertices
stack = []
for i in range(vertices):
if not visited[i]:
dfs(i, graph, visited, stack)
# Reverse stack to get the correct topological order.
return stack[::-1]
def main():
vertices = int(input("Enter the number of vertices: "))
edges = int(input("Enter the number of edges: "))
# Initialize the graph as a list of lists
graph = [[] for _ in range(vertices)]
print("Enter each edge (source destination) separated by space:")
for _ in range(edges):
u, v = map(int, input().split())
graph[u].append(v) # Directed edge from u to v
order = topological_sort(graph, vertices)
print("Topological Order:", " ".join(map(str, order)))
if __name__ == "__main__":
main()